﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business.DwzFrame;
using DotNet.Common.Command;
using NHibernate;
using NHibernate.Criterion;
using ZLCMS.Data.Model;

namespace ZLCMS.Web.SystemManage.Basics.RightConfig.Department
{
    public partial class DepList : System.Web.UI.Page
    {
        protected FrameHelper _framePage;
        private string _s_name;
        protected void Page_Load(object sender, EventArgs e)
        {
            _framePage = new FrameHelper(this);

            _s_name = Request["name"] == null ? "" : Request["name"];

            //初始化DWZ分页控件
            _framePage = new FrameHelper(this) { RecordCount = GetListCount() };

            BindList(_framePage.PageIndex, _framePage.PageSize);

        }
        /// <summary>
        /// 填充网格数据
        /// </summary>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        protected void BindList(int pageIndex, int pageSize)
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<SysDepartmentInfo>();
                SetupCondition(ref criteria);
                criteria.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize);

                grid.DataSource = criteria.List();
                grid.DataBind();
            }
        }

        /// <summary>
        /// 填充查询条件
        /// </summary>
        /// <param name="criteria"></param>
        private void SetupCondition(ref ICriteria criteria)
        {
            if (!string.IsNullOrEmpty(_s_name.Trim()))
            {
                criteria.Add(Expression.Like("DeptName", "%" + _s_name + "%"));
            }
        }

        /// <summary>
        /// 查询数据总数
        /// </summary>
        /// <returns></returns>
        protected int GetListCount()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<SysDepartmentInfo>();
                SetupCondition(ref criteria);
                object obj = criteria.SetProjection(Projections.RowCount()).UniqueResult();
                return int.Parse(obj.ToString());
            }
        }
    }
}
